package com.wkbb.sports.websocket;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.websocket.server.ServerEndpointConfig.Configurator;

/**
 * 方法一【不推荐】
 * 解决websocket下实例无法注入问题，但是它让WebSocketServer类里面的webSocketSet.size()的值不管有多少个不同的客户端连接始终为1，导致无法广播群发消息推到客户端，所以不推荐，原因未知
 * @author lbh
 */
@Component
public class EndpointConfigure extends Configurator implements ApplicationContextAware {

	private static volatile BeanFactory context;

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
         return context.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        EndpointConfigure.context = applicationContext;
    }
}
